package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface SetmealMapper {
    /**
     * 分页查询套餐列表
     *
     * @param pageQueryDTO
     * @return
     */
    Page<SetmealVO> getListSetmeal(SetmealPageQueryDTO pageQueryDTO);

    /**
     * 保存套餐信息
     *
     * @param setmeal
     */
    void saveSetmeal(Setmeal setmeal);

    /**
     * startOrStop
     *
     * @param status
     * @param id
     */
    @Update("update setmeal set status = #{status} where id=#{id};")
    void startOrStop(@Param("status") Integer status, @Param("id") Long id);

    /**
     * 获取删除的套餐信息
     *
     * @param ids
     * @return
     */
    long getCountListSetmeal(@Param("ids") List<Long> ids);

    /**
     * 删除套餐信息
     *
     * @param ids
     */
    void deleteByIds(@Param("ids") List<Long> ids);

    /**
     * 根据id获取套餐信息
     *
     * @param id
     * @return
     */
    @Select(" select s.*,\n" +
            "               c.name categoryName\n" +
            "        from setmeal s\n" +
            "                 left join\n" +
            "             category c\n" +
            "             on\n" +
            "                 s.category_id = c.id where s.id=#{id};")
    SetmealDTO getById(Long id);

    /**
     * 更新套餐信息
     *
     * @param setmeal
     */
    void updateSetmeal(Setmeal setmeal);

    /**
     * 分类下是否关联套餐
     *
     * @param id
     * @return
     */
    @Select("select count(*) from setmeal where category_id = #{id}")
    long getCountCategoryId(Long id);

    /**
     * 获取分类下的所有套餐
     *
     * @param categoryId
     * @return
     */
    @Select("select s.* from category c left join setmeal s on c.id = s.category_id where c.status=1 and s.status=1 and category_id=#{categoryId};")
    List<Setmeal> list(Long categoryId);

    /**
     * 判断套餐是否重复
     *
     * @param name
     * @return
     */
    @Select("select count(*) from setmeal where name = #{name}")
    long getCountSetmealByName(String name);

    /**
     * 判断分类下是否重复
     *
     * @param categoryId
     * @param name
     * @return
     */
    @Select("select count(*) from setmeal where category_id = #{categoryId} and name = #{name}")
    long countByCategoryIdAndName(@Param("categoryId") Long categoryId, @Param("name") String name);

    @Select("select s.* from category c left join setmeal s on c.id = s.category_id where c.status=1 and s.status=1")
    List<Setmeal> listAllSetmeal();
    /**
     * 根据条件统计套餐数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
